package pearson.automation.api;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;

/**
 * API calls for courses interaction.
 */
public class CoursesAPI extends BaseAPICall {

    @Override
    protected final String formatLogMsg(final String message) {
        return String.format("CoursesAPI: %s", message);
    }

    /**
     * Get a list of courses.
     * @param userID
     *          ID of the user
     * @return a list of course ids
     */
    public final ArrayList<String> getItems(final String userID) {
    info(String.format("getItems for %s user", userID));
        String urlTemplate = String.format(
                "rel/api/user/%1$s/courses?results=1000", userID);
        Document doc = performAPICall(urlTemplate, null, true);
        return getAPICallResults(doc, "course", "id");
    }

    /**
     * Delete a course.
     * @param userID
     *          User ID
     * @param itemID
     *          Course ID
     */
    public final void deleteItem(final String userID, final String itemID) {
    info(String.format("deleteItem(%s) for %s user", itemID, userID));
        String urlTemplate = String.format(
                "rel/api/user/%1$s/courses/delete", userID);
        HashMap<String, String> parameters = new HashMap<String, String>();
            parameters.put("courseid", itemID);
        performAPICall(urlTemplate, parameters, true);
    }

    /**
     * Delete all courses for user.
     * @param userID
     *          User ID
     */
    public final void deleteAllItems(final String userID) {
    info(String.format("deleteAllItems for %s user", userID));
        String urlTemplate = "rel/api/user/deleteUserCourses";
        HashMap<String, String> parameters = new HashMap<String, String>();
            parameters.put("userid", userID);
        performAPICall(urlTemplate, parameters, true);
    }

    /**
     * Create a new course from DevRy template.
     * @param userID
     *          User ID
     * @return course ID
     */
    public final String createDevRyCourse(final String userID) {
    info(String.format("createDevRyCourse for %s user", userID));
        String urlTemplate = String.format(
                "rel/api/user/%1$s/courses/add_book", userID);
        HashMap<String, String> parameters = new HashMap<String, String>();
            parameters.put("bookref", "t001-template-devry-8week-course");
        Document doc = performAPICall(urlTemplate, parameters, true);
        ArrayList<String> ids = getAPICallResults(doc, "course", "id");
        if (ids.size() > 0) {
            return ids.get(0);
        } else {
            fatal("No items returned by API");
        }
        return null;
    }

    /**
     * Rename a course.
     * @param userID
     *          User ID
     * @param courseID
     *          Course ID
     * @param courseName
     *          New name for the course
     */
    public final void renameCourse(
            final String userID,
            final String courseID,
            final String courseName) {
    info(String.format("renameCourse(%1$s,%2$s) for %3$s user",
            courseID, courseName, userID));
        String url = String.format(
                "rel/api/user/%1$s/course/%2$s/rename", userID, courseID);
        HashMap<String, String> parameters = new HashMap<String, String>();
            parameters.put("new_course_name", courseName);
        performAPICall(url, parameters, true);
    }

    /**
     * Get a list of modules for course.
     * @param userID
     *          User ID
     * @param courseID
     *          Course ID
     * @return an array of module ids
     */
    public final ArrayList<String> getModules(
            final String userID,
            final String courseID) {
    info(String.format("getModules(%1$s) for %2$s user", courseID, userID));
        String url = String.format(
                "rel/api/user/%1$s/course/%2$s", userID, courseID);
        Document doc = performAPICall(url, null, true);
        return getAPICallResults(doc, "module", "id");
    }

    /**
     * Add the asset to the specified module TOC.
     * @param userID
     *      User ID
     * @param courseID
     *      Course ID
     * @param moduleID
     *      Module ID
     * @param assetID
     *      Asset ID
     */
    public final void addAssetToModule(
            final String userID,
            final String courseID,
            final String moduleID,
            final String assetID) {
    info(String.format("addAssetToModule(%1$s,%2$s,%3$s) for %4$s user",
            courseID, moduleID, assetID, userID));
        String url = String.format(
                "rel/api/user/%1$s/course/%2$s/module/%3$s/assets/add",
                    userID, courseID, moduleID);
        HashMap<String, String> parameters = new HashMap<String, String>();
            parameters.put("assetid", assetID);
        performAPICall(url, parameters, true);
    }

    /**
     * Add asset to section contents.
     * @param userID
     *      User ID
     * @param courseID
     *      Course ID
     * @param moduleID
     *      Module ID
     * @param sectionID
     *      Section ID
     * @param assetID
     *      Asset ID
     */
    public final void addAssetToContents(
            final String userID,
            final String courseID,
            final String moduleID,
            final String sectionID,
            final String assetID) {
        info(String.format(
                "addAssetToContents(%1$s,%2$s,%3$s, %4$s) for %5$s user",
                    courseID, moduleID, sectionID, assetID, userID));
        String url = String.format(
          "rel/api/user/%1$s/course/%2$s/"
        + "module/%3$s/section/%4$s/contents/add",
              userID, courseID, moduleID, sectionID);
        HashMap<String, String> parameters = new HashMap<String, String>();
            parameters.put("library_item_id", assetID);
        performAPICall(url, parameters, true);
    }

    /**
     * Get a list of assets in module TOC.
     * @param userID
     *      User ID
     * @param courseID
     *      Course ID
     * @param moduleID
     *      Module ID
     * @return a list of asset ids
     */
    public final ArrayList<String> getAssetsInModule(
            final String userID,
            final String courseID,
            final String moduleID) {
        info(String.format("getAssetsInModule(%1$s, %2$s) for %3$s user",
                    courseID, moduleID, userID));
        String url = String.format(
                "rel/api/user/%1$s/course/%2$s/module/%3$s/assets",
                    userID, moduleID, courseID);
        Document doc = performAPICall(url, null, true);
        return getAPICallResults(doc, "module", "id");
    }

    /**
     * Delete asset from module TOC.
     * @param userID
     *      User ID
     * @param courseID
     *      Course ID
     * @param moduleID
     *      Module ID
     * @param assetID
     *      Asset ID to be removed
     */
    public final void removeAssetFromModule(
            final String userID,
            final String courseID,
            final String moduleID,
            final String assetID) {
        info(String.format(
                "removeAssetFromModule(%1$s,%2$s,%3$s) for %4$s user",
                    courseID, moduleID, assetID, userID));
        String url = String.format(
                "rel/api/user/%1$s/course/%2$s/module/%3$s/asset/delete",
                    userID, courseID, moduleID);
        HashMap<String, String> parameters = new HashMap<String, String>();
            parameters.put("itemid", assetID);
        performAPICall(url, parameters, true);
    }

    /**
     * Create a blank section in module.
     * @param userID
     *      User ID
     * @param courseID
     *      Course ID
     * @param moduleID
     *      Module ID
     * @return new section ID
     */
    public final String addBlankSection(
            final String userID,
            final String courseID,
            final String moduleID) {
        info(String.format(
                "addBlankSection(%1$s, %2$s) for %3$s user",
                    courseID, moduleID, userID));
        String url = String.format(
                "rel/api/user/%1$s/course/%2$s/module/%3$s/sections/add",
                userID, courseID, moduleID);
        HashMap<String, String> parameters = new HashMap<String, String>();
            parameters.put("title", "A blank section");
            parameters.put("subtitle", "Section subtitle");
            parameters.put("price", "20");
            parameters.put("role", "body");
            parameters.put("isPearson", "false");
        Document doc = performAPICall(url, parameters, true);
        ArrayList<String> ids = getAPICallResults(doc, "section", "id");
        if (ids.size() > 0) {
            return ids.get(0);
        } else {
            fatal("No items returned by API");
        }
        return null;
    }
}
